﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace TPLDFAgent
{
    class Program
    {
        static void Main(string[] args)
        {
            //var publisherConfig = new SubscriberConfiguration(
            //  "Publisher1", "Enterprise", "net.tcp://localhost:50000", null);
            //var subscriberConfig = new SubscriberConfiguration(
            //    "Subscriber1", "Enterprise", "net.tcp://localhost:50000", null);
            using (var publisherBlock = new SOABlock("Topic1"))//(publisherConfig, SubscriberOptions.None))
            {
                using (var subscriberBlock = new SOABlock("Topic1"))//(subscriberConfig, SubscriberOptions.None))
                {
                    var outputBlock = new ActionBlock<ESBMessage>(message =>
                            {
                                Console.Out.WriteLine("RECEIVED: {0}", message.Text);
                            });
                    using (subscriberBlock.LinkTo(outputBlock))
                    {
                        publisherBlock.Completion.ContinueWith(t => subscriberBlock.Complete());
                        subscriberBlock.Completion.ContinueWith(t => outputBlock.Complete());

                        publisherBlock.Connect();
                        subscriberBlock.Connect();

                        Console.Error.WriteLine("Enter the messages to publish.");
                        var line = Console.In.ReadLine();
                        while (!string.IsNullOrEmpty(line))
                        {
                            publisherBlock.Post(new ESBMessage("Topic1", line));                            
                            line = Console.In.ReadLine();
                            
                        }

                        publisherBlock.Complete();
                        outputBlock.Completion.Wait();
                    }
                }
            }
        }
    }
}
